package com.yinhe.music.base;

import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;

import java.util.HashMap;
import java.util.List;

public class CustomTabAdapter extends FragmentPagerAdapter {
    private List<Fragment> mFragments;
    private String[] mFragmentTitles;
    private HashMap<Integer, Boolean> mUpdateFragments= new HashMap<>();
    private FragmentManager mFragmentManager;

    public CustomTabAdapter(FragmentManager fm, List<Fragment> mFragments, String[] mFragmentTitles) {
        super(fm);
        mFragmentManager = fm;
        this.mFragmentTitles = mFragmentTitles;
        mFragments.clear();
        mUpdateFragments.clear();
        this.mFragments.addAll(mFragments);
    }

    public List<Fragment> getListFragment(){
        return mFragments;
    }

    public void setListNeedUpdate(List<Fragment> fragments) {
        mFragments.clear();
        if (fragments != null) {
            mFragments.addAll(fragments);
        }
        mUpdateFragments.clear();
        for (int i = 0; i < mFragments.size(); i++) {
            mUpdateFragments.put(i, true);
        }
    }


    @Override
    public Fragment getItem(int position) {
        if(mFragments.size() < position){
            return null;
        }
        return mFragments.get(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }


    @Override
    public int getCount() {
        if (mFragmentTitles == null) {
            return 0;
        }
        return mFragmentTitles.length;
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
        try {
            super.restoreState(state, loader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
